package com.yilian.xlmusic.android;

import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import androidx.annotation.NonNull;
import androidx.databinding.DataBindingUtil;
import androidx.recyclerview.widget.LinearLayoutManager;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.listener.OnItemClickListener;
import com.yilian.xlmusic.android.base.MyBaseActivity;
import com.yilian.xlmusic.android.databinding.ActivityMoreCategoryBinding;
import com.yilian.xlmusic.android.utils.net.AppConstants;
import com.yilian.xlmusic.android.view.adapter.MCategoryAdapter;
import com.yilian.xlmusic.android.view.bean.CategoryBean;
import com.yilian.xlmusic.android.view.bean.MusicCategoryBean;
import java.util.List;
import io.reactivex.rxjava3.android.schedulers.AndroidSchedulers;
import rxhttp.RxHttp;

public class MoreCategoryActivity extends MyBaseActivity {
    ActivityMoreCategoryBinding binding;
    @Override
    public int getNetType() {
        return 0;
    }


    @Override
    public int getLayoutResId() {
        return R.layout.activity_more_category;
    }

    @Override
    public void initView(Bundle var1) {
        binding = DataBindingUtil.setContentView(this,R.layout.activity_more_category);
        setTitleView();
        showDialog();
        binding.recyclerView.setLayoutManager(new LinearLayoutManager(this));
        binding.backBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });

        getCategoryResource();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        //更新适配器的内容
        if (AppConstants.changeItems.size() > 0) {
            AppConstants.refreshView(AppConstants.changeItems.get(0));
        }
    }

    private void setTitleView() {
        hideTitleBar();
        setIsFullScreen(true, true, R.color.color_000000);
        setStatusBarTextColor2(Color.parseColor("#000000"));
        setBackEnable(false);
    }

    private void getCategoryResource() {
        AppConstants.METHOD_NAME = "所有的分类列表：";
        RxHttp.get("/ylmusic-gateway/category")
                .addHeader(AppConstants.ACCESS_TOKEN_KEY, AppConstants.ACCESS_TOKEN)
                .asClass(CategoryBean.class)
                .observeOn(AndroidSchedulers.mainThread())  //主线程回调观察者
                .subscribe(result -> {
                    List<MusicCategoryBean> data = result.getData();
                    MCategoryAdapter mCategoryAdapter = new MCategoryAdapter();
                    mCategoryAdapter.setList(data);
                    binding.recyclerView.setAdapter(mCategoryAdapter);
                    if (data.size() >= 15){
                        View inflate = View.inflate(MoreCategoryActivity.this, R.layout.none, null);
                        mCategoryAdapter.addFooterView(inflate);
                    }

                    mCategoryAdapter.setOnItemClickListener(new OnItemClickListener() {
                        @Override
                        public void onItemClick(@NonNull BaseQuickAdapter<?, ?> adapter, @NonNull View view, int position) {
                            //更新适配器的内容
                            if (AppConstants.changeItems.size() > 0) {
                                AppConstants.refreshView(AppConstants.changeItems.get(0));
                            }
                            Intent intent = new Intent(MoreCategoryActivity.this, MoreMusicActivity.class);
                            intent.putExtra("category_id", data.get(position).getId());
                            startActivity(intent);
                        }

                    });
                    if (isShowDialog()){
                        closeDialog();
                        binding.recyclerView.setVisibility(View.VISIBLE);
                    }
                    binding.recyclerView.setVisibility(View.VISIBLE);
                }, throwable -> {
                    Log.e("请求失败了", "请求失败了" + throwable);
                });
    }
}